package com.space.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.space.base.ApiResponse;
import com.space.base.ErrorCode;
import com.space.config.ConfigUtil;
import com.space.config.ConfigurationJdbc;
import com.space.config.DB;
import com.space.config.Tables;
import com.space.entity.UiConfiguration;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import java.io.*;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/template")
@Slf4j
@Api("配置接口")
public class DMConfigController {
    @Autowired
    private ConfigurationJdbc configurationJdbc;


    @PostConstruct
    public void init() {



//        List<UiConfiguration> vs = new ArrayList<>();
        UiConfiguration t = new UiConfiguration("page2-1","{\"tempId\": \"page2-1\", \"category\": \"default\",\"description\": \"\", \"title\": {\"text\": \"实时俯仰角\", \"param\": {\"digit\": 0, \"unit\": \"°\"},\"textStyle\": {\"top\": \"auto\", \"right\": \"auto\", \"bottom\": \"auto\", \"left\": \"auto\", \"color\": \"#ffffff\", \"fontFamily\": \"sans-serif\", \"fontSize\": 36, \"fontWeight\": \"900\", \"isGradient\": true, \"background\": \"linear-gradient(180deg, #87d0ff 0%, #ffffff 72%, #ffffff 100%)\", \"backgroundClip\": \"text\", \"-webkitBackgroundClip\": \"text\", \"-webkitTextFillColor\": \"transparent\"}},\"chart\": {\"legend\": {\"show\": true, \"top\": \"20px\", \"right\": \"28px\", \"bottom\": \"auto\", \"left\": \"auto\", \"itemGap\": 10, \"itemWidth\": 30, \"itemHeight\": 4, \"textStyle\": {\"fontFamily\": \"sans-serif\", \"fontSize\": 24, \"fontWeight\": \"900\"},\"icon\": \"rect\", \"data\": []},\"grid\": {\"left\": \"3%\", \"right\": \"6%\", \"bottom\": \"6%\", \"top\": \"18%\", \"containLabel\": true},\"xAxis\": [{\"show\": true, \"type\": \"value\", \"name\": \"时间\", \"nameTextStyle\": {\"color\": \"#ffffffb3\", \"fontSize\": 18, \"padding\": [0, 0, 0, -5]},\"min\": 0, \"max\": 1600, \"interval\": null, \"axisLine\": {\"show\": true, \"lineStyle\": {\"color\": \"#FFFFFF\", \"width\": 3}},\"axisLabel\": {\"show\": true, \"textStyle\": {\"fontSize\": 18, \"color\": \"#ffffffb3\"},\"showMinLabel\": false, \"showMaxLabel\": true, \"align\": \"left\"},\"splitLine\": {\"show\": false, \"lineStyle\": {\"type\": \"dashed\", \"color\": \"#ffffffb3\", \"width\": 1}}}],\"yAxis\": [{\"type\": \"value\", \"name\": \"角度(°)\", \"nameTextStyle\": {\"color\": \"#ffffffb3\", \"fontSize\": 18, \"padding\": [0, 0, 0, -110]},\"min\": null, \"max\": null, \"splitNumber\": 5, \"axisLine\": {\"show\": false, \"lineStyle\": {\"color\": \"#fff\", \"width\": 3}},\"axisTick\": {\"show\": false},\"axisLabel\": {\"textStyle\": {\"fontSize\": 18, \"color\": \"#ffffffb3\"}},\"splitLine\": {\"show\": true, \"lineStyle\": {\"type\": \"dashed\", \"color\": \"#ffffffb3\", \"width\": 1}}}],\"tooltip\": {\"show\": true, \"trigger\": \"axis\", \"axisPointer\": {\"lineStyle\": {\"color\": \"#57617B\"}}},\"series\": [{\"type\": \"line\", \"name\": \"理论俯仰角\", \"smooth\": true, \"showSymbol\": false, \"itemStyle\": {\"color\": \"#FFFFFF\"},\"lineStyle\": {\"type\": \"solid\", \"width\": 3},\"z\": 2}]}}");
        UiConfiguration t1 = new UiConfiguration("page1-2","{\"tempId\": \"page1-2\",\"category\": \"default\", \"description\": \"主动段-火箭速高曲线\", \"title\": {\"text\": \"实时飞行高度\", \"param\": {\"digit\": 0, \"unit\": \"m/s\"},\"textStyle\": {\"top\": \"auto\", \"right\": \"auto\", \"bottom\": \"auto\", \"left\": \"auto\", \"color\": \"#ffffff\", \"fontFamily\": \"sans-serif\", \"fontSize\": 36, \"fontWeight\": \"900\", \"isGradient\": true, \"background\": \"linear-gradient(180deg, #87d0ff 0%, #ffffff 72%, #ffffff 100%)\", \"backgroundClip\": \"text\", \"-webkitBackgroundClip\": \"text\", \"-webkitTextFillColor\": \"transparent\"}},\"chart\": {\"legend\": {\"show\": true, \"top\": \"20px\", \"right\": \"28px\", \"bottom\": \"auto\", \"left\": \"auto\", \"itemGap\": 10, \"itemWidth\": 30, \"itemHeight\": 4, \"textStyle\": {\"fontFamily\": \"sans-serif\", \"fontSize\": 24, \"fontWeight\": \"900\"},\"icon\": \"rect\", \"data\": []},\"grid\": {\"top\": \"80px\", \"right\": \"4%\", \"bottom\": \"3%\", \"left\": \"3%\", \"containLabel\": true},\"xAxis\": [{\"show\": true, \"type\": \"value\", \"name\": \"时间\", \"nameTextStyle\": {\"color\": \"#ffffffb3\", \"fontSize\": 18, \"padding\": [0, 0, 0, -5]},\"min\": 0, \"max\": 1600, \"interval\": null, \"axisLine\": {\"show\": true, \"lineStyle\": {\"color\": \"#FFFFFF\", \"width\": 3}},\"axisLabel\": {\"show\": true, \"textStyle\": {\"fontSize\": 18, \"color\": \"#ffffffb3\"},\"showMinLabel\": false, \"showMaxLabel\": true, \"align\": \"\"},\"splitLine\": {\"show\": false, \"lineStyle\": {\"type\": \"dashed\", \"color\": \"#ffffffb3\", \"width\": 1}}}],\"yAxis\": [{\"type\": \"value\", \"name\": \"高度(km)\", \"nameTextStyle\": {\"color\": \"#ffffffb3\", \"fontSize\": 18, \"padding\": [0, 0, 0, -110]},\"min\": null, \"max\": null, \"splitNumber\": 5, \"axisLine\": {\"show\": false, \"lineStyle\": {\"color\": \"#fff\", \"width\": 3}},\"axisTick\": {\"show\": false},\"axisLabel\": {\"textStyle\": {\"fontSize\": 18, \"color\": \"#ffffffb3\"}},\"splitLine\": {\"show\": true, \"lineStyle\": {\"type\": \"dashed\", \"color\": \"#ffffffb3\", \"width\": 1}}}],\"tooltip\": {\"show\": true, \"trigger\": \"axis\", \"axisPointer\": {\"lineStyle\": {\"color\": \"#57617B\"}}},\"series\": [{\"type\": \"line\", \"name\": \"理论高度\", \"smooth\": true, \"showSymbol\": false, \"itemStyle\": {\"color\": \"#FFFFFF\"},\"lineStyle\": {\"type\": \"solid\", \"width\": 3},\"z\": 2},{\"type\": \"line\", \"name\": \"实际高度\", \"smooth\": true, \"showSymbol\": false, \"itemStyle\": {\"color\": \"#FF6A00\"},\"lineStyle\": {\"type\": \"solid\", \"width\": 3},\"z\": 2}]}}");
        UiConfiguration t2 = new UiConfiguration("page1-1","{\"tempId\": \"page1-1\", \"category\": \"default\",\"description\": \"主动段-火箭速高曲线\", \"title\": {\"text\": \"实时飞行速度\", \"param\": {\"digit\": 0, \"unit\": \"m/s\"},\"textStyle\": {\"top\": \"auto\", \"right\": \"auto\", \"bottom\": \"auto\", \"left\": \"auto\", \"color\": \"#ffffff\", \"fontFamily\": \"sans-serif\", \"fontSize\": 36, \"fontWeight\": \"900\", \"isGradient\": true, \"background\": \"linear-gradient(180deg, #87d0ff 0%, #ffffff 72%, #ffffff 100%)\", \"backgroundClip\": \"text\", \"-webkitBackgroundClip\": \"text\", \"-webkitTextFillColor\": \"transparent\"}},\"chart\": {\"legend\": {\"show\": true, \"top\": \"20px\", \"right\": \"28px\", \"bottom\": \"auto\", \"left\": \"auto\", \"itemGap\": 10, \"itemWidth\": 30, \"itemHeight\": 4, \"textStyle\": {\"fontFamily\": \"sans-serif\", \"fontSize\": 24, \"fontWeight\": \"900\"},\"icon\": \"rect\", \"data\": []},\"grid\": {\"top\": \"80px\", \"right\": \"4%\", \"bottom\": \"3%\", \"left\": \"3%\", \"containLabel\": true},\"xAxis\": [{\"show\": true, \"type\": \"value\", \"name\": \"时间\", \"nameTextStyle\": {\"color\": \"#ffffffb3\", \"fontSize\": 18, \"padding\": [0, 0, 0, -5]},\"min\": 0, \"max\": 1600, \"interval\": null, \"axisLine\": {\"show\": true, \"lineStyle\": {\"color\": \"#FFFFFF\", \"width\": 3}},\"axisLabel\": {\"show\": true, \"textStyle\": {\"fontSize\": 18, \"color\": \"#ffffffb3\"},\"showMinLabel\": false, \"showMaxLabel\": true, \"align\": \"\"},\"splitLine\": {\"show\": false, \"lineStyle\": {\"type\": \"dashed\", \"color\": \"#ffffffb3\", \"width\": 1}}}],\"yAxis\": [{\"type\": \"value\", \"name\": \"速度(m/s)\", \"nameTextStyle\": {\"color\": \"#ffffffb3\", \"fontSize\": 18, \"padding\": [0, 0, 0, -110]},\"min\": null, \"max\": null, \"splitNumber\": 5, \"axisLine\": {\"show\": false, \"lineStyle\": {\"color\": \"#fff\", \"width\": 3}},\"axisTick\": {\"show\": false},\"axisLabel\": {\"textStyle\": {\"fontSize\": 18, \"color\": \"#ffffffb3\"}},\"splitLine\": {\"show\": true, \"lineStyle\": {\"type\": \"dashed\", \"color\": \"#ffffffb3\", \"width\": 1}}}],\"tooltip\": {\"show\": true, \"trigger\": \"axis\", \"axisPointer\": {\"lineStyle\": {\"color\": \"#57617B\"}}},\"series\": [{\"type\": \"line\", \"name\": \"理论速度\", \"smooth\": true, \"showSymbol\": false, \"itemStyle\": {\"color\": \"#FFFFFF\"},\"lineStyle\": {\"type\": \"solid\", \"width\": 3},\"z\": 2},{\"type\": \"line\", \"name\": \"实际速度\", \"smooth\": true, \"showSymbol\": false, \"itemStyle\": {\"color\": \"#FF6A00\"},\"lineStyle\": {\"type\": \"solid\", \"width\": 3},\"z\": 2}]}}");

        if(DB.db1.data.equals(Tables.activeDB)) {
            configurationJdbc.create(Tables.getCreateSql(DB.db1.data));

            List<UiConfiguration> vs = configurationJdbc.get();
            if(vs.isEmpty()){
                configurationJdbc.insert(t.data,t.id);
                configurationJdbc.insert(t1.data,t1.id);
                configurationJdbc.insert(t2.data,t2.id);
            }

        }
        if(DB.db2.data.equals(Tables.activeDB)) {
            configurationJdbc.create(Tables.getCreateSql(DB.db2.data));

            List<UiConfiguration> vs = configurationJdbc.get();
            if(vs.isEmpty()){
                configurationJdbc.insert(t.data,t.id);
                configurationJdbc.insert(t1.data,t1.id);
                configurationJdbc.insert(t2.data,t2.id);
            }
        }
        if(DB.db3.data.equals(Tables.activeDB)) {
            configurationJdbc.create(Tables.getCreateSql(DB.db3.data));

            List<UiConfiguration> vs = configurationJdbc.get();
            if(vs.isEmpty()){
                configurationJdbc.insert(t.data,t.id);
                configurationJdbc.insert(t1.data,t1.id);
                configurationJdbc.insert(t2.data,t2.id);
            }
        }
        if(DB.db4.data.equals(Tables.activeDB)) {
            configurationJdbc.create(Tables.getCreateSql(DB.db4.data));

            List<UiConfiguration> vs = configurationJdbc.get();
            if(vs.isEmpty()){
                configurationJdbc.insert(t.data,t.id);
                configurationJdbc.insert(t1.data,t1.id);
                configurationJdbc.insert(t2.data,t2.id);
            }
        }
        if(DB.db5.data.equals(Tables.activeDB)) {
            configurationJdbc.create(Tables.getCreateSql(DB.db5.data));

            List<UiConfiguration> vs = configurationJdbc.get();
            if(vs.isEmpty()){
                configurationJdbc.insert(t.data,t.id);
                configurationJdbc.insert(t1.data,t1.id);
                configurationJdbc.insert(t2.data,t2.id);
            }
        }

    }

    @PostMapping("/add")
    @ApiOperation("buildFile")
    public ApiResponse<String> buildFile(@RequestBody String v){
        try {
            JSONObject obj= JSON.parseObject(v, JSONObject.class);

            configurationJdbc.insert(obj.getString("data"),obj.getString("id"));

            ApiResponse<String> result = new ApiResponse();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            ApiResponse<String> result = new ApiResponse();
            result.setReCode(ErrorCode.ERROR);
            result.setReMsg(ErrorCode.ERROR_Msg);

            return result;
        }


    }

    @PostMapping("/update")
    @ApiOperation("update")
    public ApiResponse<String> update(@RequestBody String v){
        try {
            JSONObject obj= JSON.parseObject(v, JSONObject.class);

            configurationJdbc.update(obj.getString("id"),obj.getString("data"));

            ApiResponse<String> result = new ApiResponse();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            ApiResponse<String> result = new ApiResponse();
            result.setReCode(ErrorCode.ERROR);
            result.setReMsg(ErrorCode.ERROR_Msg);

            return result;
        }


    }


    @PostMapping("/removeByIds")
    @ApiOperation("deleteFile")
    public ApiResponse<String> deleteFile(@RequestBody String v){
        try {
            JSONObject obj= JSON.parseObject(v, JSONObject.class);

            JSONArray ids= obj.getJSONArray("ids");
            Iterator<Object> i = ids.iterator();
            while (i.hasNext()){
                Object j=  i.next();
                configurationJdbc.delete((String) j);
            }

            ApiResponse<String> result = new ApiResponse();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            ApiResponse<String> result = new ApiResponse();
            result.setReCode(ErrorCode.ERROR);
            result.setReMsg(ErrorCode.ERROR_Msg);

            return result;
        }
    }



    @GetMapping("/removeAll")
    @ApiOperation("deleteAllFile")
    public ApiResponse<String> deleteAllFile(){
        try {

            configurationJdbc.deleteAll();

            ApiResponse<String> result = new ApiResponse();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            ApiResponse<String> result = new ApiResponse();
            result.setReCode(ErrorCode.ERROR);
            result.setReMsg(ErrorCode.ERROR_Msg);

            return result;
        }
    }

    @PostMapping("/findById")
    @ApiOperation("findFileById")
    public ApiResponse<String> findFileById(@RequestBody String v){
        try {
            JSONObject obj= JSON.parseObject(v, JSONObject.class);

            List<UiConfiguration> value = configurationJdbc.getById(obj.getString("id"));
            String v1="";
            if(!value.isEmpty()) v1=value.get(0).data;
            ApiResponse<String> result = new ApiResponse(v1);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            ApiResponse<String> result = new ApiResponse();
            result.setReCode(ErrorCode.ERROR);
            result.setReMsg(ErrorCode.ERROR_Msg);

            return result;
        }
    }

    @GetMapping("/findAll")
    @ApiOperation("findAll")
    public ApiResponse<String> findAll(){
        List<String> all = new ArrayList<>();
        try {

            for(UiConfiguration obj :configurationJdbc.get()){
                all.add(obj.getData());
            }

            ApiResponse<String> result = new ApiResponse(all);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            ApiResponse<String> result = new ApiResponse();
            result.setReCode(ErrorCode.ERROR);
            result.setReMsg(ErrorCode.ERROR_Msg);

            return result;
        }
    }

    @PostMapping("/excute")
    @ApiOperation("excute")
    public ApiResponse<String> excute(@RequestBody String v){
        try {
            JSONObject obj= JSON.parseObject(v, JSONObject.class);
            ApiResponse<String> result = new ApiResponse(configurationJdbc.excute(obj.getString("data")));
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            ApiResponse<String> result = new ApiResponse();
            result.setReCode(ErrorCode.ERROR);
            result.setReMsg(ErrorCode.ERROR_Msg);

            return result;
        }
    }



}


